<template>
  <div class="dashboard-container">
    <!-- github角标 -->
    <github-corner class="github-corner" />

    <el-card shadow="never">
      <el-row justify="space-between">
        <el-col :span="18" :xs="24">
          <div class="flex h-full items-center">
            <img class="w-20 h-20 mr-5 rounded-full" :src="userStore.user.avatar + '?imageView2/1/w/80/h/80'" />
            <div>
              <p>{{ greetings }}</p>
              <p class="text-sm text-gray">欢迎登陆宣和艺网管理系统</p>
            </div>
          </div>
        </el-col>

        <el-col :span="6" :xs="24">
          <div class="flex h-full items-center justify-around">
            <!-- <el-statistic v-for="item in statisticData" :key="item.key" :value="item.value">
              <template #title>
                <div class="flex items-center">
                  <svg-icon :icon-class="item.iconClass" size="20px" />
                  <span class="text-[16px] ml-1">{{ item.title }}</span>
                </div>
              </template>
</el-statistic> -->
          </div>
        </el-col>
      </el-row>
    </el-card>

    <!-- 数据卡片 -->
    <el-row :gutter="10" class="mt-3">
      <el-col :xs="24" :sm="12" :lg="6" v-for="(item, index) in cardData" :key="index">
        <el-card shadow="never">
          <template #header>
            <div class="flex items-center justify-between">
              <span class="text-[var(--el-text-color-secondary)]">{{
                item.title
              }}</span>
              <!-- <el-tag :type="item.tagType">
                {{ item.tagText }}
              </el-tag> -->
            </div>
          </template>

          <!-- <div class="flex items-center justify-between mt-5">
            <div class="text-lg text-right">
              {{ Math.round(item.count) }}
            </div>
            <svg-icon :icon-class="item.iconClass" size="2em" />
          </div> -->

          <div class="flex items-center justify-between mt-5 text-sm text-[var(--el-text-color-secondary)]">
            <span> {{ item.dataDesc }} </span>
            <span> {{ item.count }} </span>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- Echarts 图表 -->
    <el-row :gutter="10" class="mt-3">
      <el-col :xs="24" :sm="12" :lg="12" class="mb-2" v-for="item in chartData" :key="item">
        <component :is="chartComponent(item)" :id="item" height="400px" width="100%"
          class="bg-[var(--el-bg-color-overlay)]" />
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import type { EpPropMergeType } from "element-plus/es/utils/vue/props/types";
defineOptions({
  name: "Dashboard",
  inheritAttrs: false,
});

import { useUserStore } from "@/store/modules/user";
import { useTransition, TransitionPresets } from "@vueuse/core";

const userStore = useUserStore();
const date: Date = new Date();

const greetings = computed(() => {
  const hours = date.getHours();
  if (hours >= 6 && hours < 8) {
    return "晨起披衣出草堂，轩窗已自喜微凉🌅！";
  } else if (hours >= 8 && hours < 12) {
    return "上午好，" + userStore.user.nickname + "！";
  } else if (hours >= 12 && hours < 18) {
    return "下午好，" + userStore.user.nickname + "！";
  } else if (hours >= 18 && hours < 24) {
    return "晚上好，" + userStore.user.nickname + "！";
  } else {
    return "偷偷向银河要了一把碎星，只等你闭上眼睛撒入你的梦中，晚安🌛！";
  }
});

const duration = 0;

// 库房数
const amount = ref(0);
const amountOutput = useTransition(amount, {
  duration: duration,
  transition: TransitionPresets.easeOutExpo,
});
amount.value = 0;

// 主机数
const visitCount = ref(0);
const visitCountOutput = useTransition(visitCount, {
  duration: duration,
  transition: TransitionPresets.easeOutExpo,
});
visitCount.value = 0;

// 数值设备
const dauCount = ref(0);
const dauCountOutput = useTransition(dauCount, {
  duration: duration,
  transition: TransitionPresets.easeOutExpo,
});
dauCount.value = 0;

// 警报设备
const orderCount = ref(0);
const orderCountOutput = useTransition(orderCount, {
  duration: duration,
  transition: TransitionPresets.easeOutExpo,
});
orderCount.value = 0;

// 右上角数量
const statisticData = ref([
  {
    value: 99,
    iconClass: "message",
    title: "警报总数",
    key: "message",
  },
  {
    value: 50,
    iconClass: "todolist",
    title: "警报中",
    suffix: "/100",
    key: "upcoming",
  },
  {
    value: 10,
    iconClass: "project",
    title: "已停止警报",
    key: "project",
  },
]);

interface CardProp {
  title: string;
  tagType: EpPropMergeType<
    StringConstructor,
    "primary" | "success" | "info" | "warning" | "danger",
    unknown
  >;
  tagText: string;
  count: any;
  dataDesc: string;
  iconClass: string;
}
// 卡片数量
const cardData = ref<CardProp[]>([
  {
    title: "机构数量",
    tagType: "danger",
    tagText: "季",
    count: orderCountOutput,
    dataDesc: "",
    iconClass: "order",
  },
  {
    title: "艺术家数量",
    tagType: "success",
    tagText: "",
    count: visitCountOutput,
    dataDesc: "",
    iconClass: "visit",
  },
  {
    title: "艺术品数量",
    tagType: "success",
    tagText: "日",
    count: dauCountOutput,
    dataDesc: "已部署温湿度设备数量",
    iconClass: "ip",
  },
  {
    title: "静态模板数量",
    tagType: "primary",
    tagText: "月",
    count: amountOutput,
    dataDesc: "",
    iconClass: "money",
  },
]);
// 图表数据
const chartData = ref(["BarChart", "PieChart"]);
const chartComponent = (item: string) => {
  return defineAsyncComponent(() => import(`./components/${item}.vue`));
};

onMounted(() => {
  initData();
});

function initData() {
  // AlertMessageAPI.getDashboardInfo().then((data) => {
  //   for (let i = 0; i < data.length; i++) {
  //     let item = data[i];
  //     console.log(item, 111);
  //     switch (item.type) {
  //       case 1:
  //         statisticData.value[1].value = item["num"];
  //         break;
  //       case 2:
  //         statisticData.value[2].value = item["num"];
  //         break;
  //       case 3:
  //         statisticData.value[0].value = item["num"];
  //         break;
  //       case 4:
  //         orderCount.value = item["num"];
  //         break;
  //       case 5:
  //         visitCount.value = item["num"];
  //         break;
  //       case 6:
  //         dauCount.value = item["num"];
  //         break;
  //       case 7:
  //         amount.value = item["num"];
  //         break;
  //     }
  //   }
  // });
  console.log("初始化数据");
}
</script>

<style lang="scss" scoped>
.dashboard-container {
  position: relative;
  padding: 24px;

  .user-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }

  .github-corner {
    position: absolute;
    top: 0;
    right: 0;
    z-index: 1;
    border: 0;
  }

  .data-box {
    display: flex;
    justify-content: space-between;
    padding: 20px;
    font-weight: bold;
    color: var(--el-text-color-regular);
    background: var(--el-bg-color-overlay);
    border-color: var(--el-border-color);
    box-shadow: var(--el-box-shadow-dark);
  }

  .svg-icon {
    fill: currentcolor !important;
  }
}
</style>
